package td.pt;

import java.util.ArrayList;
import java.util.Collections;

import td.client.place.NameTokens;
import td.client.presenter.RootPresenter;
import td.domain.Category;
import td.domain.DemoStore;
import td.domain.util.StatisticalComparison;
import td.pt.ui.CategoryListingEntry;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class CategoryPresenter extends
		Presenter<CategoryPresenter.MyView, CategoryPresenter.MyProxy> {

	public interface MyView extends View {

		public Label getPageTitle();

		public RadioButton getTopRead();

		public RadioButton getTopTaggings();

		public RadioButton getTopReferenced();

		public CategoryListingEntry getCategory1();

		public CategoryListingEntry getCategory2();

		public CategoryListingEntry getCategory3();

		public CategoryListingEntry getCategory4();

		public CategoryListingEntry getCategory5();

		public CategoryListingEntry getCategory6();
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.categories)
	public interface MyProxy extends ProxyPlace<CategoryPresenter> {
	}

	@Inject
	public CategoryPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, RootPresenter.SLOT_content, this);
	}

	@Override
	protected void onBind() {
		super.onBind();

		getView().getTopRead().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				refresh(StatisticalComparison.NUMBER_OF_THOUGHT_READS);
			}
		});
		getView().getTopTaggings().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				refresh(StatisticalComparison.NUMBER_OF_TAGGINGS);
			}
		});
		getView().getTopReferenced().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				refresh(StatisticalComparison.NUMBER_OF_REFERENCES);
			}
		});
	}

	@Override
	protected void onReset() {
		super.onReset();

		switch (Category.currentComparisonType) {
		case NUMBER_OF_THOUGHT_READS:
			getView().getTopRead().setValue(true);
			break;
		case NUMBER_OF_TAGGINGS:
			getView().getTopTaggings().setValue(true);
			break;
		case NUMBER_OF_REFERENCES:
			getView().getTopReferenced().setValue(true);
			break;
		default:
			Category.currentComparisonType = StatisticalComparison.NUMBER_OF_THOUGHT_READS;
			getView().getTopRead().setValue(true);
			break;
		}
		refresh(Category.currentComparisonType);
	}

	private void refresh(StatisticalComparison comparisonType) {
		Category.currentComparisonType = comparisonType;

		ArrayList<Category> categories = new ArrayList<Category>(
				DemoStore.db().allCategoryList);
		Collections.sort(categories);

		int numCategories = categories.size() > 6 ? 6 : categories.size();

		switch (numCategories) {
		case 6:
			getView().getCategory6().setCategory(categories.get(5),
					comparisonType);
		case 5:
			getView().getCategory5().setCategory(categories.get(4),
					comparisonType);
		case 4:
			getView().getCategory4().setCategory(categories.get(3),
					comparisonType);
		case 3:
			getView().getCategory3().setCategory(categories.get(2),
					comparisonType);
		case 2:
			getView().getCategory2().setCategory(categories.get(1),
					comparisonType);
		case 1:
			getView().getCategory1().setCategory(categories.get(0),
					comparisonType);
		}
	}
}
